package yesjava.news.utils;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ServletUploadUtil {
    static  ThreadLocal<Map<String,Object>>  threadLocal = new ThreadLocal<>();
    private static Map<String,Object> getRequestMap(HttpServletRequest request){
        Map<String,Object>  rs = threadLocal.get();
        if(rs==null){
            rs = parseMultipartRequest(request);
            threadLocal.set(rs);
        }
        return rs;
    }


    //一个请求只能解释一次！！
    private static Map<String,Object> parseMultipartRequest(HttpServletRequest request){
        Map<String,Object> rsMap = new HashMap<>();
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        if(isMultipart){
            DiskFileItemFactory factory = new DiskFileItemFactory();
            ServletContext servletContext = request.getSession().getServletContext();
            File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
            factory.setRepository(repository);

            ServletFileUpload upload = new ServletFileUpload(factory);
            upload.setHeaderEncoding("UTF-8");
            try {
                List<FileItem> items = upload.parseRequest(request);
                for (FileItem item : items) {
                    String fieldName = item.getFieldName();
                    if(item.isFormField()){
                        if(rsMap.get(fieldName)==null){
                            ArrayList<String> list = new ArrayList<>();
                            rsMap.put(fieldName,list);
                        }
                        ((ArrayList<String>)(rsMap.get(fieldName))).add(item.getString("UTF-8"));
                    }else{
                        if(rsMap.get(fieldName)==null){
                            ArrayList<FileItem> list = new ArrayList<>();
                            rsMap.put(fieldName,list);
                        }
                        ((ArrayList<FileItem>)(rsMap.get(fieldName))).add(item);
                    }
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return  rsMap;
    }



    public static String getParameter(String name,HttpServletRequest request){
        String rs = null;
        Map<String, Object> map = getRequestMap(request);
        ArrayList<String> list = (ArrayList<String>) map.get(name);
        if(list!=null && list.size()>0){
            rs =  list.get(0);
        }
        return rs;
    }

    public static String[] getParameterValues(String name,HttpServletRequest request){
        String[] rs = null;
        Map<String, Object> map = getRequestMap(request);
        ArrayList<String> list = (ArrayList<String>) map.get(name);
        if(list!=null && list.size()>0){
            rs = list.toArray(new String[]{});
        }
        return rs;
    }

    public static FileItem getParameterFileItem(String name,HttpServletRequest request){
        FileItem rs = null;
        Map<String, Object> map = getRequestMap(request);
        ArrayList<FileItem> list = (ArrayList<FileItem>) map.get(name);
        if(list!=null && list.size()>0){
            rs =  list.get(0);
        }
        return rs;
    }

    public static FileItem[] getParameterFileItemVaues(String name,HttpServletRequest request){
        FileItem[] rs = null;
        Map<String, Object> map = getRequestMap(request);
        ArrayList<FileItem> list = (ArrayList<FileItem>) map.get(name);
        if(list!=null && list.size()>0){
            rs = list.toArray(new FileItem[]{});
        }
        return rs;
    }
}
